package ldh.fx.adminfx.controller;

import com.jfoenix.controls.JFXPopup;
import javafx.animation.FadeTransition;
import javafx.animation.ParallelTransition;
import javafx.animation.SequentialTransition;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import ldh.fx.adminfx.commpont.LNavPane;
import ldh.fx.adminfx.util.StageUtil;

import java.net.URL;
import java.util.ResourceBundle;

public class HomeHeadController implements Initializable {

    @FXML private VBox userContent;
    @FXML private Button userBtn;
    @FXML private HBox head;
    @FXML private Label userLabel;
    @FXML private Button tongleBtn;

    private JFXPopup userPopupPane;
    private LNavPane lNavPane = null;
    private ParallelTransition parallelTransition;
    private ParallelTransition parallelTransition2;

    public void setLNavPane(LNavPane lNavPane) {
        this.lNavPane = lNavPane;
    }

    @FXML
    public void tongleLeftPane(ActionEvent e) {
        if (lNavPane != null) {
            lNavPane.tongleLeftPane();
            if (lNavPane.isBigger()) {
                parallelTransition2.playFromStart();
            } else {
                parallelTransition.playFromStart();
            }
        }
    }

    @FXML public void showUserPopup(ActionEvent e) {
        if (!userContent.getStyleClass().contains("nav-user-popup")) {
            userContent.getStyleClass().add("nav-user-popup");
        }
        userPopupPane.show(userBtn, JFXPopup.PopupVPosition.TOP, JFXPopup.PopupHPosition.RIGHT);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initPopup();
        userPopupPane = new JFXPopup(userContent);
        parallelTransition = buildTransition(tongleBtn, userLabel, 0, 1.0, 0);
        parallelTransition2 = buildTransition(tongleBtn, userLabel, 120, 0, 1.0);
    }

    private void initPopup() {
        head.getChildren().remove(userContent);
    }

    @FXML public void settingBtn(ActionEvent e) {
        if (userPopupPane.isShowing()) {
            userPopupPane.hide();
        }
    }

    @FXML public void profileBtn(ActionEvent actionEvent) {
//        LxDialog ldhDialog = new LxDialog(StageUtil.STAGE, "test", 500d, 300d);
//        VBox box = new VBox();
//        box.setStyle("-fx-padding: 5");
//        for (int i=0; i<10; i++) {
//            box.getChildren().add(new Label("asdfasfd"));
//        }
//        ldhDialog.setContentPane(box);
//        ldhDialog.show();
//        if (userPopupPane.isShowing()) {
//            userPopupPane.hide();
//        }
    }

    @FXML public void logoutBtn(ActionEvent actionEvent) {
        StageUtil.exit();
    }

    private ParallelTransition buildTransition(Node node1, Node node2, double value1, double value2, double value3) {
        TranslateTransition t1 = new TranslateTransition(Duration.millis(200));
//        t1.setFromX(200);
        t1.setToX(value1);
        t1.setNode(node1);
        FadeTransition f1 = new FadeTransition(Duration.millis(150));
        f1.setNode(node2);
        f1.setFromValue(value2);
        f1.setToValue(value3);
        ParallelTransition sq = new ParallelTransition();
        sq.getChildren().addAll(t1, f1);
        return sq;
    }
}
